package com.m3839.sdk.ym.ui

import android.app.Activity
import android.content.pm.ActivityInfo
import android.graphics.Color
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.m3839.sdk.ym.base.YMBaseDialogFragment
import com.m3839.sdk.ym.constant.YMStringConstant
import com.m3839.sdk.ym.core.YMManager
import com.m3839.sdk.ym.core.YMStorage
import com.m3839.sdk.ym.ext.getId

/**
 * @description: YMContactDialogFragment
 *
 * @author ChenBaoyang
 * @date 6/16/21 16:54
 */
class YMContactDialogFragment : YMBaseDialogFragment() {

    private lateinit var closeBtn: ImageView
    private lateinit var titleTv: TextView
    private lateinit var contentTv: TextView

    private var defaultOrientation = 0

    companion object{

        private const val FRAGMENT_TAG = "com.m3839.sdk.ym:contact"

        fun show(activity: Activity) {
            val fragment = YMContactDialogFragment()
            fragment.show(activity.fragmentManager, FRAGMENT_TAG)
        }
    }

    override fun parseArgs() {
        defaultOrientation = YMStorage.instance.getDefaultOrientation(activity, -1)
    }

    override fun layoutName() =
        if (defaultOrientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) "ym_page_contact_land" else "ym_page_contact"

    override fun initView(view: View?) {
        closeBtn = view!!.findViewById(getId("btn_close"))
        titleTv = view.findViewById(getId( "tv_title"))
        contentTv = view.findViewById(getId( "tv_content"))
    }

    override fun setContent() {
        titleTv.text = YMStringConstant.YM_FCM_GET_PASSWORD

        val contact: String? = YMManager.instance.getContact()
        val source: String = YMStringConstant.getPasswordContact(contact!!)
        val spannable = SpannableString(source)
        val start = source.indexOf(contact)
        val end = start + contact.length
        val span = ForegroundColorSpan(Color.parseColor("#23c268"))
        spannable.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        contentTv.text = spannable
    }

    override fun initListener() {
        closeBtn.setOnClickListener { dismiss() }
    }

}